'''
Created on Sep 1, 2012

@author: LongDT
'''
import csv 

#trainName = "../../data/train.csv"
trainName = "../../data/trainNew.csv"
testName = "../../data/test.csv"
#trainFeatureName = "../../data/trainFeatures.csv"
#testFeatureName = "../../data/testFeatures.csv"
trainFeatureName = "../../data/trainFullFeatureData.csv"
testFeatureName = "../../data/testFullFeatureData.csv"

### CREATE THE TRAINING 
def createMergedFeaturesAPart(trainFeatureName,testFeatureName,trainFeatureMerge,testFeatureMerge):
    
    f = open(trainFeatureMerge,"w")
    f1 = open(trainFeatureName)
    lines = f1.readlines()
    spamReader = csv.reader(open(trainName, 'rb'), delimiter=',')
    count = 0; 
    count1 = 0;
    label = ""
    for line in spamReader:
        count+=1
        if (count >1):
            if ((count-1) % 6==0):
                feature = lines[count1].strip()
                count1+=1
                f.write(feature+","+str(label)+"\n")
            t = line[1].strip()
            if (line[0]=='1'):
                temp = line[1]
                label = 5
                if (temp.strip()=="population"): label = 1
                if (temp.strip()=="intervention"): label = 2
                if (temp.strip()=="background"): label = 3
                if (temp.strip()=="outcome"): label = 4
                #if ("study" in temp.strip()): label = 5
                if (temp.strip()=="other"): label = 6
    
    f.close()
    f1.close()
    
    ### DO THE SAME FOR THE TEST ############
    f = open(testFeatureMerge,"w")
    f1 = open(testFeatureName)
    lines = f1.readlines()
    
    spamReader = csv.reader(open(testName, 'rb'), delimiter=',')
    count = 0;
    count1 = 0;
     
    for line in spamReader:
        count+=1
        if (count >1):
            if ((count-1) % 6==1):
                feature = lines[count1].strip()
                count1+=1
                f.write(feature+"\n")
    f.close()
    f1.close()
    
def createMergedFeaturesFull(trainFeatureName,testFeatureName,trainFeatureMerge,testFeatureMerge):
    
    f = open(trainFeatureMerge,"w")
    f1 = open(trainFeatureName)
    lines = f1.readlines()
    spamReader = csv.reader(open(trainName, 'rb'), delimiter=',')
    count = 0; 
    count1 = 0;
    label = ""
    for line in spamReader:
        count+=1
        if (count >1):
            if ((count-1) % 6==1):
                feature = lines[count1].strip()
                count1+=1
            # Convert the label to number 
            temp = line[1]
            label = 0
            if (temp.strip()=="population"): label = 1
            if (temp.strip()=="intervention"): label = 2
            if (temp.strip()=="background"): label = 3
            if (temp.strip()=="outcome"): label = 4
            if (temp.strip()=="study design"): label = 5
            if (temp.strip()=="other"): label = 6

            f.write(feature+","+str(label)+"," + str(line[0])+ "\n")
    f.close()
    f1.close()
    
    ### DO THE SAME FOR THE TEST ############
    f = open(testFeatureMerge,"w")
    f1 = open(testFeatureName)
    lines = f1.readlines()
    
    spamReader = csv.reader(open(testName, 'rb'), delimiter=',')
    count = 0;
    count1 = 0;
     
    for line in spamReader:
        count+=1
        if (count >1):
            if ((count-1) % 6==1):
                feature = lines[count1].strip()
                count1+=1
            # Convert the label to number 
            temp = line[0]
            label = 0
            if (temp.strip()=="population"): label = 1
            if (temp.strip()=="intervention"): label = 2
            if (temp.strip()=="background"): label = 3
            if (temp.strip()=="outcome"): label = 4
            if (temp.strip()=="study design"): label = 5
            if (temp.strip()=="other"): label = 6

            f.write(feature+","+str(label)+"\n")
    f.close()
    f1.close()
